package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.OrderSettings;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderSettingServiceImpl
 * @Description TODO
 * @Author lc
 * @Date 2021/11/15 20:21
 * @Version 1.0
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public List<OrderSettings> getOrderSettingsByMonth(String currentYearAndMonth) {
        List<OrderSetting> orderSettingsByMonth = orderSettingDao.getOrderSettingsByMonth(currentYearAndMonth);
        if (orderSettingsByMonth != null) {
            List<OrderSettings> orderSettingsList = new ArrayList<>();
            //循环遍历封装数据
            for (int i = 0; i < orderSettingsByMonth.size(); i++) {
                Date date = orderSettingsByMonth.get(i).getOrderDate();
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                // 获得当前日期是本月的第几天
                int day = cal.get(Calendar.DAY_OF_MONTH);
                //封装数据
                OrderSettings orderSettings = new OrderSettings();
                orderSettings.setDate(day);
                orderSettings.setNumber(orderSettingsByMonth.get(i).getNumber());
                orderSettings.setReservations(orderSettingsByMonth.get(i).getReservations());
                orderSettingsList.add(orderSettings);
            }
            return orderSettingsList;
        }
        return null;
    }

    @Transactional
    @Override
    public Boolean updateOrderSettingNumber(OrderSetting orderSetting) {
        try {
            String date = DateUtils.parseDate2String(orderSetting.getOrderDate());
            int number = orderSetting.getNumber();
            return this.updateOrInsert(date, number);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Transactional
    @Override
    public void uploadFile(String dateValue, Integer numValue) {
        this.updateOrInsert(dateValue, numValue);
    }

    private Boolean updateOrInsert(String date, Integer number) {
        //查询当天有没有预约数据,有就修改没有就新增
        OrderSetting byDate = orderSettingDao.findByDate(date);
        if (null != byDate) {
            //修改可预约人数时不能小于已预约人数
            if (number < byDate.getReservations()) {
                return false;
            }
            return orderSettingDao.updateOrderSetting(date, number);
        } else {
            return orderSettingDao.insertOrderSetting(date, number);
        }
    }


    //===========================================================
    //根据预约日期 查询 预约表
    @Override
    public OrderSetting findByDate(Date date) {
        OrderSetting orderSetting = this.orderSettingDao.findByDateAPP(date);

        return orderSetting;
    }
    //更改预约人数
    @Transactional
    @Override
    public Boolean update(OrderSetting orderSetting) {
        return this.orderSettingDao.update(orderSetting)>0;
    }

}
